angular.module('BaseControl')

.directive("btnfind", ['$compile','BaseConst', 'BaseEnum', 'BaseModule', 'BaseFunction', 'BaseClass',
    function($compile, BaseConst, BaseEnum, BaseModule, BaseFunction, BaseClass) {
    return {
        restrict: "E",	// 指令是一个元素 (并非属性)，备选项有"A"、"E" 和 "C"， "M" ,分别代表attribute、element、class和comment（默认值为"A"）
        scope: {
            module : '=?',
            change: '&'
        },
        templateUrl: 		// 替换HTML (使用scope中的变量)
            'modules/Base/client/views/BtnFind.html',
        replace: true,			// 使用模板替换原始标记
        transclude: true,		// 不复制原始HTML内容
        controller: ['$scope', function($scope) {
            $scope.GirdModule;
            $scope.ID = BaseFunction.getGuid();

            var isCreateDataGrid = false;
            var createDataGrid = function(){
                if (!isCreateDataGrid) {
                    var dataGridHtml = '<DataGrid module="GirdModule" rowdblclick="rowDblClick()" isedit="false" height="430px">';
                    for(var i=0; i<$scope._DataGridElement.children.length; i++){
                        dataGridHtml += $scope._DataGridElement.children[i].outerHTML.replace(/fcolumn/g, "Column").replace(/enum=\"/g, "enum=\"$parent.");
                    }
                    dataGridHtml += '</DataGrid>';

                    $compile(dataGridHtml)($scope, function (clone, $scope) {
                        $scope._DataGridElement.appendChild(clone[0]);
                    });
                    isCreateDataGrid = true;
                }
                else{
                    $scope.GirdModule.emitReloadEvent();
                }
            };
            $scope.btnClick = function() {
                if($scope.module.EntityName == undefined || $scope.module.EntityName == null || $scope.module.EntityName.trim() == ''){
                    event.stopPropagation();
                    return;
                }
                createDataGrid();
            };
            $scope.btnOKClick = function(){
                if($scope.GirdModule.FocusedRecord == null){
                    BaseFunction.alert('请选择记录！');
                    event.stopPropagation();
                    return;
                }

                if($scope.module.FocusedRecord == null || $scope.module.FocusedRecord._id != $scope.GirdModule.FocusedRecord._id){
                    $scope.module.Records = [];
                    $scope.module._Records.push($scope.GirdModule.FocusedRecord);
                    $scope.module.FocusedRecord = $scope.module.Records[0];
                    if($scope.module.AfterOnLoad != null){
                        $scope.module.AfterOnLoad([$scope.GirdModule.FocusedRecord]);
                    }
                    $scope.change();
                }

                $('#' + $scope.ID).modal('hide');
            };
            $scope.rowDblClick = function(){
                $scope._BtnOKElement.click();
            };
        }],
        link: function (scope, element, attrs) {
            scope.Enabled = true;
            scope.Visible = true;
            scope.WindowTitle = '参照';
            scope.Condition = scope.module.Condition;
            scope.OrderBy = scope.module.OrderBy;
            scope.Populate = scope.module.Populate;

            scope._DataGridElement = element[0].children[1].children[0].children[0].children[1]; //angular.element(element[0].children[1].children[0].children[0].children[1]);
            scope._BtnOKElement = element[0].children[1].children[0].children[0].children[2].children[1];
            if (attrs["windowtitle"] != undefined && attrs["windowtitle"].trim() != "") {
                scope.WindowTitle = attrs["windowtitle"].trim();
            }
            if (attrs["condition"] != undefined && attrs["condition"].trim() != "") {
                scope.Condition = eval("(" + attrs["condition"].trim() + ")");
            }
            if (attrs["orderby"] != undefined && attrs["orderby"].trim() != "") {
                scope.OrderBy = eval("(" + attrs["orderby"].trim() + ")");
            }
            if (attrs["populate"] != undefined && attrs["populate"].trim() != "") {
                if(attrs["populate"].trim().substring(0, 1) == "{"){
                    scope.Populate = eval("(" + attrs["populate"].trim() + ")");
                }
                else{
                     scope.Populate = attrs["populate"].trim();
                }
            }

            scope.GirdModule = BaseModule.initModule(scope, scope.module.EntityName);
            scope.GirdModule.Condition = scope.Condition;
            scope.GirdModule.OrderBy = scope.OrderBy;
            scope.GirdModule.Populate = scope.Populate;
            scope.GirdModule.PageSize = 13;
            scope.GirdModule.LoadMode = BaseEnum.LoadMode.CacheAndPaging;

            scope.$watch(
                function(){
                    return attrs["enabled"];
                },
                function(){
                    if (attrs["enabled"] != undefined && attrs["enabled"].trim().toLocaleLowerCase() == "false") {
                        scope.Enabled = false;
                    }
                    else{
                        scope.Enabled = true;
                    }
                }
            );

            scope.$watch(
                function(){
                    return attrs["visible"]
                },
                function(){
                    if (attrs["visible"] != undefined && attrs["visible"].trim().toLocaleLowerCase() == "false") {
                        scope.Visible = false;
                    }
                    else{
                        scope.Visible = true;
                    }
                }
            );
        }
    }
}]);